package shop.ynubeggar.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import shop.ynubeggar.exception.CustomException;
import shop.ynubeggar.mapper.CategoryMapper;
import shop.ynubeggar.model.dataobject.CategoryDO;
import shop.ynubeggar.model.valueobject.CategoryVO;
import shop.ynubeggar.request.CreateCategoryRequest;
import shop.ynubeggar.request.UpdateCategoryRequest;
import shop.ynubeggar.service.AdminCategoryService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@DS("yeat_product")
public class AdminCategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements AdminCategoryService {

    @Resource
    CategoryMapper categoryMapper;


    @Override
    @DS("yeat_product")
    public void createCategory(CreateCategoryRequest createCategoryRequest) {
        categoryMapper.insert(new CategoryDO(createCategoryRequest));
    }

    @Override
    @DS("yeat_product")
    public Map<String, Object> listAllCategory(int page, int size) {
        IPage<CategoryDO> categoryDOIPage = page(new Page<>(page, size));
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_page", categoryDOIPage.getPages());
        map.put("total_record", categoryDOIPage.getTotal());
        map.put("data", categoryDOIPage.getRecords().stream().map(obj -> {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtil.copyProperties(obj, categoryVO);
            return categoryVO;
        }).collect(Collectors.toList()));
        return map;
    }

    @Override
    public CategoryVO findOneCategory(long categoryId) {
        CategoryDO categoryDO = getById(categoryId);
        if (categoryDO == null) {
            log.error("findCategory:res:false,id:{}", categoryId);
            throw new CustomException(HttpStatus.NOT_FOUND, "category(id=" + categoryId + " does not exist");
        }
        CategoryVO categoryVO = new CategoryVO();
        BeanUtil.copyProperties(categoryDO, categoryVO);
        return categoryVO;
    }

    @Override
    public void deleteOneCategory(long categoryId) {
        if (!removeById(categoryId)) {
            log.error("removeCategory:res:false,id:{}", categoryId);
            throw new CustomException(HttpStatus.NOT_FOUND, "category(id=" + categoryId + " does not exist");
        }
        log.info("removeCategory:res:true,id:{}", categoryId);
    }

    @Override
    public void updateCategory(long id, UpdateCategoryRequest updateCategoryRequest) {
        CategoryDO categoryDO = getById(id);
        if (categoryDO == null) {
            log.error("updateCategory:res:false,id:{}", id);
            throw new CustomException(HttpStatus.NOT_FOUND, "category(id=" + id + " does not exist");
        }
        BeanUtil.copyProperties(updateCategoryRequest, categoryDO);
        updateById(categoryDO);
        log.error("updateCategory:res:true,id:{},category:{}", id, categoryDO);
    }


}
